/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年6月3日         luwei
 */

package com.coocaa.launcher.wallpaper;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.RemoteException;
import android.view.View;

import com.coocaa.launcher.util.FrescoBitmapFactory;
import com.coocaa.launcher.util.Utils;
import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.framework.utils.ImageUtils;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecontent.api.ContentApi;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_WallpaperList;
import com.tianci.appstore.R;

import java.util.ArrayList;
import java.util.List;


public class WallpaperManager {
    private List<OnBackgroundChangeListener> mBackgroundChangeListeners = new ArrayList<OnBackgroundChangeListener>();
    private static final String BACKGROUND = "background";
    private static final String INNER_BACKGROUND = "inner_background";
    private int mCurrentBg = R.drawable.bg_1;
    public static int mWidth = 0;
    public static int mHeight = 0;
    private Resources mResources = null;
    private Context mContext = null;
    private static WallpaperManager mInstance = null;
    private RP_WallpaperList.WallPaper_Item current = null;
    private boolean changeSuccess = false;


    private WallpaperManager() {

    }

    public synchronized static WallpaperManager getInstance() {
        if (mInstance == null) {
            mInstance = new WallpaperManager();
        }
        return mInstance;
    }

    public void init(Context context) {
        this.mContext = context;
        mResources = mContext.getResources();
        mWidth = Utils.getScreenWidth(context);
        mHeight = Utils.getScreenHeight(context);
        if (mWidth > 1280) {
            mWidth = 1280;
            mHeight = 720;
        }
    }

    /**
     * TODO (需要切换背景的地方需要实现这个接口)
     *
     * @author luwei
     * @version 2014年12月30日
     */
    public interface OnBackgroundChangeListener {
        public void onBackgroundChange();
    }

    public void addBackgroundChangeListener(OnBackgroundChangeListener listener) {
        if (!mBackgroundChangeListeners.contains(listener)) {
            mBackgroundChangeListeners.add(listener);
        }
    }

    public void removeBackgroundChangeListener(OnBackgroundChangeListener listener) {
        if (mBackgroundChangeListeners.contains(listener)) {
            mBackgroundChangeListeners.remove(listener);
        }
    }

    public interface InnerWallpaperChangeListener {
        public void innerWallpaperChange(int pos);
    }


    public void changeAppBackground(RP_WallpaperList.WallPaper_Item item) {
        current = item;
        for (OnBackgroundChangeListener listener : mBackgroundChangeListeners) {
            listener.onBackgroundChange();
        }
        changeSuccess = true;
    }

    public void getBackground(int defaultId, FrescoBitmapFactory.OnBitmapLoadListener listener) {
        getBackground(defaultId, mWidth, mHeight, listener);
    }


    public void getBackground(final int defaultId, final int width, final int height, final FrescoBitmapFactory.OnBitmapLoadListener listener) {

        AsyncTask.runOnThread(new Runnable() {
            @Override
            public void run() {
                if (current == null)
                    current = ContentApi.getCurrentWallpaper();
                LogUtils.d("wallpaper", "the current is " + current);
                if (current != null) {
                    FrescoBitmapFactory.decodeBitmap(mContext, Uri.parse(current.bigUrl), width, height, listener);
                } else if (listener != null) {
                    listener.loadSuccess(ImageUtils.decodeSampledBitmapFromResource(mResources, defaultId, width, height), "");
                }
            }
        });
    }

    public void getResourceBitmap(final int id, final int width, final int height, final FrescoBitmapFactory.OnBitmapLoadListener listener) {
        AsyncTask.runOnThread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = ImageUtils.decodeSampledBitmapFromResource(mResources, id, width, height);
                if (bitmap.getWidth() != width)
                    bitmap = ImageUtils.scale(bitmap, width, height);
                if (listener != null)
                    listener.loadSuccess(bitmap, "");
            }
        });
    }

    public void getResourceBitmap(int id, FrescoBitmapFactory.OnBitmapLoadListener listener) {
        getResourceBitmap(id, mWidth, mHeight, listener);
    }

    public RP_WallpaperList.WallPaper_Item getCurrent() {
        return current;
    }

    public void setCurrent(RP_WallpaperList.WallPaper_Item item){
        this.current = item;
    }


    public int getCurrentInnerWallpaper() {
        String pos = "";
        try {
            pos = XServiceManager.getLiteConfigService().getStringValue(INNER_BACKGROUND, "");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        if (!pos.equals("")) {
            try {
                return Integer.valueOf(pos);
            } catch (Exception e) {
                return -1;
            }
        }
        return -1;
    }

    public void setBackgroundFile(View view, String path) {
        Drawable drawable = view.getBackground();
        if (drawable != null) {
            drawable.setCallback(null);
        }
        view.setBackgroundDrawable(new BitmapDrawable(ImageUtils.decodeSampledBitmapFromFile(path,
                mWidth, mHeight)));
    }

    public void destory() {
        mInstance = null;
    }

    public RP_WallpaperList.WallPaper_Item getCurrentWallpaper() {
        return current;
    }

    public boolean getChangeSuccess() {
        return changeSuccess;
    }

    public void setChangeSuccess(boolean success) {
        changeSuccess = success;
    }
}
